﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Diagnostics;
using System;
using System.Linq;
using Microsoft.Win32;
using System.Collections.Generic;
using System.IO;

public class KillApp : Editor
{
    [MenuItem("Tools/Close Unity")]
    public static void KillProcess()
    {
        string UnityPath = GetUnityRootPath();
        UnityEngine.Debug.Log(UnityPath);
        return;
        Process[] MyProcesses;
        //得到所有打开的进程  
        MyProcesses = Process.GetProcesses();
        try
        {
            foreach (Process MyProcess in MyProcesses)
            {
                UnityEngine.Debug.LogFormat("<color=yellow>{0}</color>", MyProcess.ProcessName);
                //查找是否正在运行   
                if (MyProcess.ProcessName.CompareTo("Unity") == 0)
                {
                    StartProcess(UnityPath);
                    MyProcess.Kill();
                }
            }
        }
        catch (Exception Exc)
        {
            throw Exc;
        }
    }

    public static void StartProcess(string ApplicationPath)
    {
        UnityEngine.Debug.Log("打开本地应用");
        Process foo = new Process();
        foo.StartInfo.FileName = ApplicationPath;
        foo.Start();
    }

    /// <summary>
    /// 获取单个程序的执行目录
    /// </summary>
    /// <param name="processName"></param>
    /// <returns></returns>
    public static string GetPath(string processName)
    {
        var process = Process.GetProcessesByName(processName);

        var path = string.Empty;//程序路径
        foreach (var p in process.Where(p => p.MainWindowHandle != IntPtr.Zero))
        {
            path = p.MainModule.FileName;
            break;
        }
        return path;
    }

    /// <summary>
    /// 获取所有程序的安装目录
    /// </summary>
    public static void GetAllProcess()
    {
        const string Uninstall = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        using (var registryKey = Registry.LocalMachine.OpenSubKey(Uninstall, false))
        {
            if (registryKey != null)//判断对象存在
            {
                foreach (var keyName in registryKey.GetSubKeyNames())//遍历子项名称的字符串数组
                {
                    using (var key = registryKey.OpenSubKey(keyName, false))//遍历子项节点
                    {
                        if (key != null)
                        {
                            var softwareName = key.GetValue("DisplayName", "").ToString();//获取软件名
                            var installLocation = key.GetValue("InstallLocation", "").ToString();//获取安装路径

                            if (!string.IsNullOrEmpty(installLocation))
                            {
                                UnityEngine.Debug.Log(softwareName+"  "+ installLocation);
                            }
                        }
                    }
                }
            }
        }
    }

    /// <summary>
    /// 获取已安装的软件列表
    /// </summary>
    /// <returns></returns>
    /// <remarks></remarks>
    public static List<string> GetInstalledSoftwaresList()
    {
        List<string> InstalledSoftwareList = new List<string>();
        InstalledSoftwareList.Clear();
        try
        {
            var _with1 = InstalledSoftwareList;
            //HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
            //HKEY_CURRENT_USER\Software
            RegistryKey Keys = Registry.LocalMachine.OpenSubKey("HKEY_CURRENT_USER\\Software\\");
            string[] S = Keys.GetSubKeyNames();
            foreach (string tms in S)
            {
                Keys = Registry.LocalMachine.OpenSubKey("HKEY_CURRENT_USER\\Software\\" + tms, true);
                string K = Keys.GetValue("DisplayName", "").ToString();
                if (K.Length != 0)
                {
                    _with1.Add("[" + K + "]");
                    _with1.Add(K + "/");
                }
                K = Keys.GetValue("DisplayVersion", "").ToString();
                if (K.Length != 0)
                {
                    _with1.Add("版本:/" + K);
                }
                K = Keys.GetValue("Publisher", "").ToString();
                if (K.Length != 0)
                {
                    _with1.Add("发布者:/" + K);
                }
                K = Keys.GetValue("InstallLocation", "").ToString();
                if (K.Length != 0)
                {
                    _with1.Add("安装目录:/" + K);
                }
                K = Keys.GetValue("InstallDate", "").ToString();
                if (K.Length != 0)
                {
                    _with1.Add("安装日期:/" + K);
                }
                Keys.Close();
            }
            foreach (var item in _with1)
            {
                UnityEngine.Debug.Log(item.ToString());

                if (item.IndexOf("Unity") != -1)
                {
                    UnityEngine.Debug.Log(item.ToString());
                }
            }
        }
        catch
        {
        }
        return InstalledSoftwareList;
    }


    private static string GetUnityRootPath()
    {
        var regKey = @"Unity package file\DefaultIcon";
        RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(regKey);
        string pathName = (string)registryKey.GetValue(null);  // "(Default)"  
        if (string.IsNullOrEmpty(pathName))
        {
            return null;
        }
        int index = pathName.LastIndexOf(",");
        if (index != -1)
        {
            var exepath = pathName.Substring(0, index).Replace("\"", string.Empty);
            UnityEngine.Debug.Log(exepath);
            var binpath = Path.GetDirectoryName(exepath);   //  
            UnityEngine.Debug.Log(binpath);

            var di = new DirectoryInfo(binpath);
            if (di.Parent != null)
            {
                //return di.Parent.FullName;
            }
            return exepath;
        }
        return null;
    }
}
